﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class EmployerUserGateway : DBGateway
    {
        public string SaveEmployerUser(EmployerUser employerUser)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblEmployerUser values(@Name,@Phone,@Email,@Password)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@Name", SqlDbType.NVarChar).Value = employerUser.Name;
                CommandObj.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = employerUser.Phone;
                CommandObj.Parameters.Add("@Email", SqlDbType.NVarChar).Value = employerUser.Email;
                CommandObj.Parameters.Add("@Password", SqlDbType.NVarChar).Value = employerUser.Password;
               


                CommandObj.ExecuteNonQuery();
                return "Employer user created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public bool HasThisEmployerExist(EmployerUser employerUser)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblEmpUser Where EmpEmail='{0}'",employerUser.Email);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Employeer Not Save, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }

        public bool HasThisEmailPass(EmployerUser employerUser)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblEmpUser Where EmpEmail='{0}' and Password='{1}'", employerUser.Email,employerUser.Password);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Password does not change, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }
    }
}
    
